//Name:Yifu Diao
//Reference:
//http://developer.researchadvanced.com/pages/developers_area/ebay_research_api/api_call_reference/getresearchtrenddata.html
//Input field reference:http://developer.researchadvanced.com/pages/developers_area/ebay_research_api/api_call_reference/getresearchtrenddata/getresearchtrenddata_input/getresearchtrenddata_input_field_reference.html
package edu.cmu.cs.task7.model;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;


import edu.cmu.cs.task7.databeans.ProductStat;

public class TrendResearchModel {
	 private String itemid;
	 private String AppId="Carnegie-0a89-4ae8-ad7e-5f26c9955759"; //Only production appid works.
	 private String SiteId = "0";  // Site used. Site ID 0 = US
	 private String Version = "2"; // API version; The only correct version right now
	 private String Query;
	 private String keyword;
	 private String apiCall; 
	 
     public TrendResearchModel(String keyword) throws Exception{
    	 this.keyword=keyword;
    	 Query=keyword;
    	 Query=java.net.URLEncoder.encode(Query,"UTF-8");
    	 apiCall = "http://open.api.dataunison.com/rest/?CallName=GetPriceResearch&AppID="+
         this.AppId+"&SiteId="+this.SiteId+"&Version="+this.Version+"&ResearchPeriod=30&QueryKeywords="+this.Query;
     }
     
     public ProductStat getProductStat() throws IOException, ParserConfigurationException, SAXException{
    	 ProductStat ps=new ProductStat();
    	 ps.setKeyword(keyword);
    	 URL myURL;
         InputStream in;
    	 
    	 myURL = new URL(apiCall);
		 URLConnection ucon = myURL.openConnection();
         in = ucon.getInputStream();
		  
		 DocumentBuilder builder=DocumentBuilderFactory 
		  .newInstance() 
		  .newDocumentBuilder();     
		   
		 Document doc=builder.parse(in, null);
		 
		 NodeList stats=doc.getElementsByTagName("Statistics");  // Count how many items are there 
		 String ts=doc.getElementsByTagName("TotalSales").item(0).getChildNodes().item(0).getNodeValue();	
    	 ps.setTotalSales(Double.parseDouble(ts));
    	 String tsi=doc.getElementsByTagName("TotalSoldItems").item(0).getChildNodes().item(0).getNodeValue();	
    	 ps.setSoldItems(Integer.parseInt(tsi));
    	 String asp=doc.getElementsByTagName("AverageSoldPrice").item(0).getChildNodes().item(0).getNodeValue();	
    	 ps.setAvgSoldPrice(Double.parseDouble(asp));
    	 String msp=doc.getElementsByTagName("MaxSoldPrice").item(0).getChildNodes().item(0).getNodeValue();	
    	 ps.setMaxSoldPrice(Double.parseDouble(msp));
    	 String minsp=doc.getElementsByTagName("MinSoldPrice").item(0).getChildNodes().item(0).getNodeValue();	
    	 ps.setMinSoldPrice(Double.parseDouble(minsp));
    	 
    	 return ps;
     }
}